package com.quanshu.exchange.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.quanshu.change.broadcast.PayResultBroadcatReciver;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.profile.AllOrderActivity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private IWXAPI iwxapi;
    private String url;
    private int state = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iwxapi = WXAPIFactory.createWXAPI(this, MyApplication.getInstance().getWX_ID(), false);
        initIntent();
        iwxapi.handleIntent(getIntent(), this);
    }

    private void initIntent() {
        url = getIntent().getStringExtra("url");
        state = getIntent().getIntExtra("state", 0);
        Log.d("微信支付", "initIntent: " + url);
        if (!TextUtils.isEmpty(url)) {
            Map<String, String> map = URLRequest(url);
            String partnerid = map.get("partnerid");
            String prepayid = map.get("prepayid");
            String noncestr = map.get("noncestr");
            String timestamp = map.get("timestamp");
            String sign = map.get("sign");

            PayReq request = new PayReq(); //调起微信APP的对象
            //下面是设置必要的参数，也就是前面说的参数,这几个参数从何而来请看上面说明
            request.appId = MyApplication.getInstance().getWX_ID();
            request.partnerId = partnerid;
            request.prepayId = prepayid;
            request.packageValue = "Sign=WXPay";
            request.nonceStr = noncestr;
            request.timeStamp = timestamp;
            request.sign = sign;
            iwxapi.sendReq(request);//发送调起微信的请求
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        LogUtil.print("============");
    }

    @Override
    public void onResp(BaseResp resp) {
        //成功
        if (resp.errCode == 0) {
            if (state == 0) {
                Intent intent = new Intent(WXPayEntryActivity.this, AllOrderActivity.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent();
                intent.setAction(PayResultBroadcatReciver.PAY_GOLD_RECHAGR_SUCCESSFUL);
                sendBroadcast(intent);

            }

        } else if (resp.errCode == -2) {
            ToastHelper.toast("取消支付");
        } else {
            ToastHelper.toast("支付异常");
        }
        finish();
    }


    public static Map<String, String> URLRequest(String URL) {
        Map<String, String> mapRequest = new HashMap<String, String>();

        String[] arrSplit = null;

        String strUrlParam = URL;
        if (strUrlParam == null) {
            return mapRequest;
        }
        //每个键值为一组
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");

            //解析出键值
            if (arrSplitEqual.length > 1) {
                //正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

            } else {
                if (arrSplitEqual[0] != "") {
                    //只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }
}